package com.example.lenovo.weichat;

import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private ViewPager mViewPager;
    private PagerAdapter mAdapter;

    //保存微信，通讯录，发现，我4个界面的view
    private List<View> mViews = new ArrayList<View>();

    //底部的四个按钮，微信，通讯录，发现，我
    private LinearLayout  btnChat;
    private LinearLayout  btnContact;
    private LinearLayout  btnFind;
    private LinearLayout  btnMe;

    private ImageView ivChat;
    private ImageView ivContact;
    private ImageView ivFind;
    private ImageView ivMe;

    private TextView tvChat;
    private TextView tvContact;
    private TextView tvFind;
    private TextView tvMe;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化view
        initView();
        //初始化事件
        initEvent();
    }

    public void initEvent() {
        btnChat.setOnClickListener(this);
        btnContact.setOnClickListener(this);
        btnFind.setOnClickListener(this);
        btnMe.setOnClickListener(this);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {//当viewpager滑动时，对应的底部导航按钮的图片要变化
                int currentItem = mViewPager.getCurrentItem();
                int color = Color.parseColor("#1B940A");
                resetImage();
                switch (currentItem) {
                    case 0:
                        ivChat.setImageResource(R.drawable.weixin_pressed);
                        tvChat.setTextColor(color);
                        break;
                    case 1:
                        ivContact.setImageResource(R.drawable.contact_list_pressed);
                        tvContact.setTextColor(color);
                        break;
                    case 2:
                        ivFind.setImageResource(R.drawable.find_pressed);
                        tvFind.setTextColor(color);
                        break;
                    case 3:
                        ivMe.setImageResource(R.drawable.profile_pressed);
                        tvMe.setTextColor(color);
                        break;

                    default:
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

    public void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);

        //底部按钮
        btnChat = (LinearLayout) findViewById(R.id.id_lay_chat);
        btnContact = (LinearLayout) findViewById(R.id.id_lay_contact);
        btnFind = (LinearLayout) findViewById(R.id.id_lay_find);
        btnMe = (LinearLayout) findViewById(R.id.id_lay_me);

        ivChat = (ImageView) findViewById(R.id.ivChat);
        ivContact = (ImageView) findViewById(R.id.ivContact);
        ivFind = (ImageView) findViewById(R.id.ivFind);
        ivMe = (ImageView) findViewById(R.id.ivMe);

        tvChat = (TextView) findViewById(R.id.tvChat);
        tvContact = (TextView) findViewById(R.id.tvContact);
        tvFind = (TextView) findViewById(R.id.tvFind);
        tvMe = (TextView) findViewById(R.id.tvMe);


        //通过LayoutInflater引入布局，并将布局转化为view
        LayoutInflater mInflater = LayoutInflater.from(this);//创建一个LayoutInflater对象

        View chatLayout = mInflater.inflate(R.layout.chat_layout, null);
        View contactLayout = mInflater.inflate(R.layout.contact_layout, null);
        View findLayout = mInflater.inflate(R.layout.find_layout, null);
        View settingLayout = mInflater.inflate(R.layout.me_layout, null);

        mViews.add(chatLayout);
        mViews.add(contactLayout);
        mViews.add(findLayout);
        mViews.add(settingLayout);

        mAdapter = new PagerAdapter() {
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                // TODO Auto-generated method stub
                container.removeView(mViews.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = mViews.get(position);//从mViews这个list中根据位置取出我们需要的view
                container.addView(view);//将上述的view添加到ViewGroup中
                return view;
            }

            /*
             * isViewFromObject用来判断instantiateItem(ViewGroup, int)函数所返回来的Key与一个页面视图是否是代表
             * 的同一个视图(即它俩是否是对应的，对应的表示同一个View),如果对应的是同一个View，返回True，否则返回False
             * */
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                // 返回PagerView包含的view的个数
                return mViews.size();
            }
        };
        //为ViewPager设置adapter
        mViewPager.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        resetImage();
        switch (v.getId()) {
            case R.id.id_lay_chat:
                mViewPager.setCurrentItem(0);//如果点击的是微信，就将viewpager的当前item设置为0，及切换到微信聊天的viewpager界面
                ivChat.setImageResource(R.drawable.weixin_pressed);
                break;
            case R.id.id_lay_contact:
                mViewPager.setCurrentItem(1);
                ivContact.setImageResource(R.drawable.contact_list_pressed);
                break;
            case R.id.id_lay_find:
                mViewPager.setCurrentItem(2);
                ivFind.setImageResource(R.drawable.find_pressed);
                break;
            case R.id.id_lay_me:
                mViewPager.setCurrentItem(3);
                ivMe.setImageResource(R.drawable.profile_pressed);
                break;
            default:
                break;
        }
    }

    public void resetImage() {
        int color = Color.parseColor("#7f7f7f");
        ivChat.setImageResource(R.drawable.weixin_normal);
        tvChat.setTextColor(color);

        ivContact.setImageResource(R.drawable.contact_list_normal);
        tvContact.setTextColor(color);

        ivFind.setImageResource(R.drawable.find_normal);
        tvFind.setTextColor(color);

        ivMe.setImageResource(R.drawable.profile_normal);
        tvMe.setTextColor(color);
    }
}
